package core;
import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

import org.dom4j.DocumentException;

import ui.LoadingFrame;
import util.AddJavaLibraryPath;
import util.RegeditTool;

import org.springframework.util.FileCopyUtils;

import config.ClientConfig;
import config.ServerConfig;


public class ServerMain
{
	/**
	 * @param args
	 */
	//static OneInstance oneInstance= new OneInstance();
	public static void main(String[] args)
	{
		/*
		try {
			 setAutoStart();//开机启动
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
		ServerConfig serverConfig = ServerConfig.getInstance();
		ClientConfig clientConfig = ClientConfig.getInstance();
		Global global = Global.getInstance();
		
		try {
			global.setConnectPort(clientConfig.port());
			global.setConnectAddr(clientConfig.host());
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			clientConfig.initClientConfig();
			try {
				global.setConnectPort(clientConfig.port());
				global.setConnectAddr(clientConfig.host());
				JOptionPane.showMessageDialog(null, "未发现配置信息，已启用默认配置", "客户端配置错误", JOptionPane.ERROR_MESSAGE);
			} catch (DocumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		
		
		
		try {
			global.setListenPort(serverConfig.listenport());
			global.setHeartbeat(serverConfig.heatbeat());
			
			
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			serverConfig.initServerConfig();
			
			try {
				global.setListenPort(serverConfig.listenport());
				global.setHeartbeat(serverConfig.heatbeat());
				JOptionPane.showMessageDialog(null, "未发现配置信息将启用默认配置", "服务器配置错误", JOptionPane.ERROR_MESSAGE);
			} catch (DocumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		
		
		LoadingFrame loadingFrame=new LoadingFrame();
	    loadingFrame.setLocationRelativeTo(null);
	    loadingFrame.setVisible(true);
	    Thread index = new Thread(loadingFrame,"MiniServer");
	    index.start();
		
		
		/*
		try {
			 setAutoStart();//开机启动
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ServerDisplay.getInstance().init();
		try {
			if(MinaServer.serverStart()){
				
				logger.debug("服务器启动成功......");
				
			}else
			{
				
				logger.debug("服务器启动失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.debug("服务器启动异常");
			e.printStackTrace();
		}
		ServerDisplay.getInstance().updateComponents();
		
		
		
		
		MsgProcess process = new MsgProcess();
		process.start(); 
		
		Client.startClient();
		*/
		
		
		
	}
	public static void setAutoStart() throws DocumentException, IOException {
		
		if(ServerConfig.getInstance().isCopyDLL())
			FileCopyUtils.copy(new File("lib/ICE_JNIRegistry.dll"), new File(ServerConfig.getInstance().copyDLLDest())) ;
		String JAVA_LIBRARY_PATH = System.getProperty("java.library.path") ;
		
		printLibrary("JAVA_LIBRARY_PATH", JAVA_LIBRARY_PATH) ;
		
		String ADD_DLL_PATH = new File("lib/ICE_JNIRegistry.dll").getAbsolutePath() + File.pathSeparator + JAVA_LIBRARY_PATH ;
		
		System.setProperty("java.library.path", ADD_DLL_PATH) ;
		
		printLibrary("After Add DLL LIB JAVA_LIBRARY_PATH", System.getProperty("java.library.path")) ;
		
		AddJavaLibraryPath.addDir(new File("lib").getAbsolutePath()) ;
		
		loadLibrary() ;
		
		RegeditTool.regeditDccServer() ;
		
	}
	
	private static void loadLibrary() {
		
		try {
			System.loadLibrary( "ICE_JNIRegistry" );
			}
		catch ( UnsatisfiedLinkError e )
			{
			System.err.println
				( "ERROR You have not installed the DLL named '"
					+ "ICE_JNIRegistry.DLL'.\n\t" + e.getMessage() );
			}
		catch ( SecurityException e )
			{
			System.err.println
				( "ERROR You do not have permission to load the DLL named '"
					+ "ICE_JNIRegistry.DLL'.\n\t" + e.getMessage() );
			}
			
	}
	
	public static String printLibrary(String message, String java_library_path) {
		
		String java_library_path_temp = java_library_path ;
		StringBuffer buffer = new StringBuffer() ;
		
		String[] libs = java_library_path_temp.split(File.pathSeparator) ;
		for (String lib : libs) {
			buffer.append(lib + "\n") ;
		}
		//logger.debug(message + " : \n" + buffer.toString()) ;
		return buffer.toString() ;
	}
	
}
